<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
    @open="openDialog"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      :data="permissionData"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <template #footer>
      <div style="text-align: right;">
        <el-button @click="closeAssignDialog">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { transTreeDate } from '@/utils/transTreeDate'
import { assignPerm, getRoleDetail } from '@/api/setting'
export default {
  props: {
    showAssignDialog: {
      type: Boolean
    },
    roleId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      permissionData: [] // 存储权限数据
    }
  },
  methods: {
    //   关闭弹窗
    closeAssignDialog() {
      this.$emit('close-dialog')
    },
    // 打开弹窗
    openDialog() {
      this.hgetPermiList()
      this.hGetRole()
    },
    // 获取权限列表
    async hgetPermiList() {
      const res = await getPermissionList()
      this.permissionData = transTreeDate(res)
    },
    // 获取角色详情  回显数据
    async hGetRole() {
      const res = await getRoleDetail(this.roleId)
      this.$refs.tree.setCheckedKeys(res.permIds)
    },
    // 提交
    async confirm() {
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.closeAssignDialog()
    }
  }

}
</script>

<style>

</style>
